package com.sea.finance.service.impl;

import com.sea.finance.entity.FreightTaxPredict;
import com.sea.finance.mapper.FreightTaxPredictMapper;
import com.sea.finance.service.FreightTaxPredictService;
import com.sea.supply.entity.ProductPrice;
import com.sea.supply.service.ProductPriceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;


/**
 * @author 陈鸿飞
 */
@Service
public class FreightTaxPredictServiceImpl implements FreightTaxPredictService {

    @Autowired
    private FreightTaxPredictMapper freightTaxPredictMapper;
    @Autowired
    private ProductPriceService productPriceService;

    @Override
    public List<FreightTaxPredict> findByCnd(FreightTaxPredict freightTaxPredict) {
        return freightTaxPredictMapper.findByCnd(freightTaxPredict);
    }

    @Override
    public int save(FreightTaxPredict freightTaxPredict,Double freight) {
        System.out.println(freightTaxPredict.getProductSku());
        String sku=freightTaxPredict.getProductSku();

        if(sku.equals("INHP9022XB-C63")){
            System.out.println("1111");
        }
        if(freightTaxPredict.getProductChannelName().equals("四海商舟.amazon.欧洲") ||
           freightTaxPredict.getProductChannelName().equals("四海商舟.amazon.英国")
        ){
            freightTaxPredict.setProductTax((freightTaxPredict.getProductUnitPriceCny()+freightTaxPredict.getProductFreight())  * freightTaxPredict.getCustomsRate());
        }else if(freightTaxPredict.getProductChannelName().equals("Voova.amazon.eu")){

            freightTaxPredict.setProductFreight(freightTaxPredict.getProductGrossWeight()*freight);
            freightTaxPredict.setProductTax((freightTaxPredict.getProductUnitPriceCny()+freightTaxPredict.getProductFreight())  * freightTaxPredict.getCustomsRate());
        }else {
            freightTaxPredict.setProductTax(freightTaxPredict.getProductPriceCny() * freightTaxPredict.getCoefficientCoef() * freightTaxPredict.getCustomsRate());
        }
        return freightTaxPredictMapper.insert(freightTaxPredict);
    }

    @Override
    public void predict(Double freight) {
        freightTaxPredictMapper.reload();
        List<FreightTaxPredict> list = freightTaxPredictMapper.predict(freight);
        for(FreightTaxPredict predict:list){
            ProductPrice productPrice=new ProductPrice();
            productPrice.setProductChannelName(predict.getProductChannelName());
            productPrice.setProductSku(predict.getProductSku());
//            ProductPrice price = productPriceService.findByMaxId(productPrice);
            predict.setProductPriceCny(predict.getProductPriceCny());
            predict.setProductUnitPriceCny(predict.getProductUnitPriceCny());
            save(predict,freight);
        }
    }
}
